package com.xcity.game.activity.newplayer;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.activity.newplayer.NewPlayerTask.State;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;
import naga.x.util.TimeUtils;

public class NewPlayerTaskManager {
	public static String STR_POOL_PREFIX = "np{0}";//用pool存储
	protected Player owner;
	protected int creatDay;
	//	public ConcurrentHashMap<Integer, NewPlayerTask> tid2task;
	protected List<NewPlayerTask> taskList;
	public NewPlayerTaskManager(Player player, List<NewPlayerTask> tlist) {
//		tid2task = new ConcurrentHashMap<>(200);
		taskList = new ArrayList<NewPlayerTask>();
		owner = player;
		if(player.getCreateTime() != null){
			creatDay = TimeUtils.ofDay(player.getCreateTime().getTime());
		}else{
			creatDay = 0;
		}
		if(ActivityNewPlayerService.INIT){
			if(checkInActive()){
				for(int i = 0; i < ActivityNewPlayerService.getTaskTempList().size(); i++){
					NewPlayerTaskTemplate temp = ActivityNewPlayerService.getTaskTempList().get(i);
					String key = MessageFormat.format(STR_POOL_PREFIX, temp.getId());
					int poolVal = player.getPool().getInt(key, -1);
					if(poolVal > -1){
						NewPlayerTask task = new NewPlayerTask(this, temp, poolVal);
						taskList.add(task);
					}else{
						NewPlayerTask task = new NewPlayerTask(this, temp, 0);
						taskList.add(task);
					}
				}
			}
		}
	}
	public Player getOwner() {
		return owner;
	}
	public void setOwner(Player owner) {
		this.owner = owner;
	}
	public int getCreatDay() {
		return creatDay;
	}
	
	@Deprecated
	public void cheatResetCreatDay(){
		creatDay = TimeUtils.ofDay(owner.getCreateTime().getTime()) - 7;
	}
	
	public long getCountEndSecond(){
//		long createTime = this.getOwner().getCreateTime().getTime();
		long now = TimeUpdater.getInstance().now();
		int today = TimeUpdater.getInstance().today();
		//creatDay + ActivityNewPlayerService.COUNT_DAY - today
		return (TimeUtils.getDayGap(today, creatDay + ActivityNewPlayerService.COUNT_DAY)) * 24 * 3600L - (now - TimeUtils.getZeroTimeOf(now)) / 1000L;//ActivityNewPlayerService.COUNT_DAY - 1
	}
	public long getRewardEndSecond(){
		long now = TimeUpdater.getInstance().now();
		int today = TimeUpdater.getInstance().today();
		//creatDay + ActivityNewPlayerService.REWARD_DAY - today
		return (TimeUtils.getDayGap(today, creatDay + ActivityNewPlayerService.REWARD_DAY)) * 24 * 3600L - (now - TimeUtils.getZeroTimeOf(now)) / 1000L;//ActivityNewPlayerService.REWARD_DAY - 1
	}
	
	public boolean checkInActive(){
		int today = TimeUpdater.getInstance().today();
		if(TimeUtils.checkDayDuring(creatDay, today, ActivityNewPlayerService.REWARD_DAY)){//today < creatDay + ActivityNewPlayerService.REWARD_DAY
			return true;
		}
		return false;
	}
	
	public NewPlayerTask getTaskByTempId(int tempId){
		for(int i = 0; i < taskList.size(); i++){
			if(taskList.get(i).getTemplate().getId() == tempId){
				return taskList.get(i);
			}
		}
		return null;
	}
	
	public List<NewPlayerTask> getTaskListByDay(int currentDay){
		List<NewPlayerTask> list = new ArrayList<NewPlayerTask>();
		for(int i = 0; i < taskList.size(); i++){
			if(currentDay == taskList.get(i).getTemplate().getStartDay()){
				list.add(taskList.get(i));
			}
		}
		return list;
	}
	
	public boolean clearPool(){
		List<NewPlayerTaskTemplate> taskList = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(NewPlayerTaskTemplate.class);
		for(int i = 0;i < taskList.size(); i++){
			int poolval = this.getOwner().getPool().getInt(MessageFormat.format(STR_POOL_PREFIX, taskList.get(i).getId()), -1);
			if(poolval == -1){
				this.getOwner().getPool().remove(MessageFormat.format(STR_POOL_PREFIX, taskList.get(i).getId()));
			}
		}
		return false;
	}
	
	public void handleOnChange(byte typeId, Object...params){
		if(!checkInActive()){
			return;
		}
		NewPlayerTracer tracer = NewPlayerTracerFactory.creat(typeId);
		if(tracer != null){
			for(int i = 0;i < taskList.size(); i++){
				NewPlayerTask task = taskList.get(i);
				if(task.getState() == NewPlayerTask.State.DOING && task.getTemplate().getType() == typeId){
					tracer.onChange(task, params);
					if(tracer.isDone(task)){
						task.setState(State.DONE);
					}
				}
			}
		}
	}
	
	public List<NewPlayerTask> getTasks() {
		return taskList;
	}
	
}
